package com.Servlet;

import com.DAO.StudentDao;
import com.DAO.UserDao;
import com.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    private UserDao userDao;

    @Override
    public void init() throws ServletException {
        userDao = new UserDao();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try{
            //int id = Integer.parseInt(request.getParameter("id"));
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");

            System.out.print("拿到数据");

            User user = new User();
            //user.setId(id);
            user.setUsername(username);
            user.setPassword(password);
            user.setEmail(email);

            System.out.println("开始插入数据");
            userDao.addUser(user);


            // 返回列表
            response.sendRedirect("userManagement");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "添加用户失败，请检查输入！");
            request.getRequestDispatcher("addUser.jsp").forward(request, response);
        }


    }
}
